﻿package org.xhs.report.common.ui.panel
{
    import __AS3__.vec.*;
    import baidu.lib.utils.*;
    import baidu.ui.controls.*;
    import baidu.ui.managers.*;
    import baidu.wenku.common.*;
    import baidu.wenku.common.model.*;
    import baidu.wenku.common.ui.*;
    import baidu.wenku.common.utils.*;
    import flash.display.*;
    import flash.events.*;
    import flash.text.*;

    public class SeriesPanel extends Sprite implements IPanel
    {
        protected var hasDestroyed:Boolean = true;
        protected var closeBtn:Button;
        protected var screenModel:ScreenModel;
        protected var seriesModel:SeriesModel;
        protected var _seriesFrame:Sprite;
        protected var _seriesItemContainer:Sprite;
        protected var _serieScrollPane:WenkuScrollPane;
        protected var itemArray:Vector.<Sprite>;
        public static const CLOSE_CLICK:String = "close_click";

        public function SeriesPanel()
        {
            this.init();
            return;
        }// end function

        protected function init() : void
        {
            this.seriesModel = InstanceManager.createSingletonInstance(SeriesModel);
            this.seriesModel.addEventListener(SeriesModel.OPEN_PANEL, this.openPanel);
            this.seriesModel.addEventListener(SeriesModel.CLOSE_PANEL, this.closePanel);
            this.screenModel = InstanceManager.createSingletonInstance(ScreenModel);
            this.screenModel.addEventListener(Event.RESIZE, this.screenResize);
            return;
        }// end function

        protected function screenResize(event:Event = null) : void
        {
            if (this.seriesModel.isOpen && this._seriesFrame)
            {
                this.resetPanel();
            }
            return;
        }// end function

        protected function closeBtnClick(event:MouseEvent) : void
        {
            this.screenModel.stage.removeEventListener(MouseEvent.CLICK, this.closeBtnClick);
            dispatchEvent(new Event(SeriesPanel.CLOSE_CLICK));
            this.destroy();
            event.stopPropagation();
            return;
        }// end function

        protected function openPanel(event:Event) : void
        {
            this.resetPanel();
            return;
        }// end function

        protected function closePanel(event:Event) : void
        {
            this.destroy();
            return;
        }// end function

        protected function seriesItemMouseHandler(event:MouseEvent) : void
        {
            var _loc_2:* = event.target.name;
            var _loc_3:* = int(_loc_2.split("_")[1]);
            var _loc_4:Number = 228;
            var _loc_5:Number = 24;
            var _loc_6:* = Sprite(event.target).graphics;
            var _loc_7:int = 0;
            var _loc_8:* = Sprite(event.target).numChildren;
            switch(event.type)
            {
                case MouseEvent.MOUSE_OVER:
                {
                    _loc_6.clear();
                    _loc_6.beginFill(16777215, 0.3);
                    _loc_6.drawRect(0, 0, _loc_4, _loc_5);
                    _loc_6.endFill();
                    _loc_7 = 0;
                    while (_loc_7 < _loc_8)
                    {
                        
                        if (Sprite(event.target).getChildAt(_loc_7) is TextField)
                        {
                            (Sprite(event.target).getChildAt(_loc_7) as TextField).textColor = 13034156;
                        }
                        _loc_7++;
                    }
                    break;
                }
                case MouseEvent.MOUSE_OUT:
                {
                    _loc_6.clear();
                    _loc_6.beginFill(16777215, 0);
                    _loc_6.drawRect(0, 0, _loc_4, _loc_5);
                    _loc_6.endFill();
                    _loc_7 = 0;
                    while (_loc_7 < _loc_8)
                    {
                        
                        if (Sprite(event.target).getChildAt(_loc_7) is TextField)
                        {
                            (Sprite(event.target).getChildAt(_loc_7) as TextField).textColor = 16777215;
                        }
                        _loc_7++;
                    }
                    break;
                }
                case MouseEvent.MOUSE_DOWN:
                {
                    WenkuLog.addLog(WenkuLog.LOGURL_SERIES_JUMP);
                    BrowserUtil.openNewWindow(this.seriesModel.dataArray[_loc_3].link);
                    break;
                }
                default:
                {
                    break;
                    break;
                }
            }
            event.stopPropagation();
            return;
        }// end function

        public function resetPanel() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            var _loc_8:Boolean = false;
            var _loc_10:Sprite = null;
            var _loc_11:Sprite = null;
            var _loc_12:Sprite = null;
            var _loc_13:Sprite = null;
            var _loc_14:TextField = null;
            var _loc_15:String = null;
            var _loc_16:* = undefined;
            if (!this.seriesModel.dataArray || !this.seriesModel.isOpen)
            {
                return;
            }
            this.screenModel.addStageMask();
            _loc_1 = 228;
            _loc_2 = 24;
            var _loc_3:Number = 10;
            var _loc_4:Number = 10;
            var _loc_5:Number = 6;
            var _loc_6:Number = 17;
            var _loc_7:* = Math.min(10, Math.floor((this.screenModel.stage.stageHeight - Config.SB_HEIGHT - Config.TB_HEIGHT - _loc_4 * 2 - _loc_6 * 2) / _loc_2));
            _loc_8 = this.seriesModel.seriesLength > _loc_7 ? (true) : (false);
            _loc_7 = Math.min(this.seriesModel.seriesLength, _loc_7);
            if (!this._seriesFrame)
            {
                this._seriesFrame = new Sprite();
            }
            this._seriesFrame.graphics.clear();
            this._seriesFrame.graphics.lineStyle(2, 16777215, 0.4);
            this._seriesFrame.graphics.beginFill(5395026, 0.9);
            this._seriesFrame.graphics.moveTo(0, 0);
            this._seriesFrame.graphics.lineTo(_loc_1 + _loc_5 * 2, 0);
            this._seriesFrame.graphics.lineTo(_loc_1 + _loc_5 * 2, _loc_2 * _loc_7 + _loc_6 * 2);
            this._seriesFrame.graphics.lineTo(50, _loc_2 * _loc_7 + _loc_6 * 2);
            this._seriesFrame.graphics.lineTo(50 - 6, _loc_2 * _loc_7 + _loc_6 * 2 + 6);
            this._seriesFrame.graphics.lineTo(50 - 12, _loc_2 * _loc_7 + _loc_6 * 2);
            this._seriesFrame.graphics.lineTo(0, _loc_2 * _loc_7 + _loc_6 * 2);
            this._seriesFrame.graphics.lineTo(0, 0);
            this._seriesFrame.graphics.endFill();
            addChild(this._seriesFrame);
            if (!this._serieScrollPane)
            {
                this._serieScrollPane = new WenkuScrollPane();
                this._serieScrollPane.x = _loc_5;
                this._serieScrollPane.y = 22;
                this._serieScrollPane.vScrollBar.setStyle("skin", ScrollBar_Skin2);
                this._serieScrollPane.hScrollPolicy = "off";
                this._serieScrollPane.setStyle("skin", ScrollPane_Skin2);
            }
            addChild(this._serieScrollPane);
            this._serieScrollPane.setSize(_loc_1, _loc_2 * _loc_7);
            this._serieScrollPane.vScrollPolicy = _loc_8 ? ("on") : ("off");
            if (!this._seriesItemContainer)
            {
                this._seriesItemContainer = new Sprite();
            }
            while (this._seriesItemContainer.numChildren)
            {
                
                _loc_10 = this._seriesItemContainer.getChildAt(0) as Sprite;
                this._seriesItemContainer.removeChildAt(0);
                _loc_10 = null;
            }
            this.itemArray = new Vector.<Sprite>;
            var _loc_9:int = 0;
            while (_loc_9 < this.seriesModel.seriesLength)
            {
                
                _loc_11 = new Sprite();
                _loc_11.name = "item_" + _loc_9;
                _loc_11.graphics.beginFill(0, 0);
                _loc_11.graphics.drawRect(0, 0, _loc_1, _loc_2);
                _loc_11.graphics.endFill();
                _loc_12 = new TypeIcon();
                _loc_13 = new Sprite();
                _loc_13.addChild(_loc_12[this.seriesModel.dataArray[_loc_9].doctTpe] || _loc_12["txt"]);
                _loc_11.addChild(_loc_13);
                _loc_13.x = 5;
                _loc_13.y = (_loc_2 - _loc_13.height) / 2;
                _loc_14 = new TextField();
                _loc_14.defaultTextFormat = new TextFormat("宋体", 12, 16777215);
                _loc_14.selectable = false;
                _loc_14.width = _loc_1 - 24 - (_loc_8 ? (20) : (0));
                _loc_14.height = 18;
                _loc_15 = this.seriesModel.dataArray[_loc_9].title;
                _loc_14.text = _loc_15;
                TextUtils.cutTextAutomatically(_loc_14, "...");
                _loc_11.addChild(_loc_14);
                _loc_14.x = 25;
                _loc_14.y = (_loc_2 - _loc_14.height) / 2;
                this._seriesItemContainer.addChild(_loc_11);
                this.itemArray.push(_loc_11);
                _loc_11.x = 0;
                _loc_11.y = _loc_9 * _loc_2;
                _loc_11.buttonMode = true;
                _loc_11.mouseChildren = false;
                _loc_11.addEventListener(MouseEvent.MOUSE_OVER, this.seriesItemMouseHandler);
                _loc_11.addEventListener(MouseEvent.MOUSE_OUT, this.seriesItemMouseHandler);
                _loc_11.addEventListener(MouseEvent.MOUSE_DOWN, this.seriesItemMouseHandler);
                _loc_9++;
            }
            this._serieScrollPane.content = this._seriesItemContainer;
            this._serieScrollPane.update();
            if (!this.closeBtn)
            {
                _loc_16 = new AllSkins();
                this.closeBtn = new Button();
                this.closeBtn.setStyle("skin", _loc_16["skin_seriesclose"]);
                this.closeBtn.x = _loc_1 + _loc_5 - Config.DV_WIDTH_RIGHT_SCROLLBAR;
                this.closeBtn.y = 5;
                this.closeBtn.useHandCursor = true;
                this.closeBtn.addEventListener(MouseEvent.CLICK, this.closeBtnClick);
            }
            addChild(this.closeBtn);
            this.screenModel.stage.addChild(this);
            this.screenModel.stage.addEventListener(MouseEvent.CLICK, this.closeBtnClick);
            x = 11;
            y = this.screenModel.stageHeight - Config.SB_HEIGHT - this._seriesFrame.height - 2;
            this.hasDestroyed = false;
            return;
        }// end function

        public function destroy() : void
        {
            var _loc_2:DisplayObject = null;
            if (this.hasDestroyed)
            {
                return;
            }
            this.closeBtn.removeEventListener(MouseEvent.CLICK, this.closeBtnClick);
            var _loc_1:int = 0;
            while (_loc_1 < this.itemArray.length)
            {
                
                (this.itemArray[_loc_1] as Sprite).removeEventListener(MouseEvent.MOUSE_OVER, this.seriesItemMouseHandler);
                (this.itemArray[_loc_1] as Sprite).removeEventListener(MouseEvent.MOUSE_OUT, this.seriesItemMouseHandler);
                (this.itemArray[_loc_1] as Sprite).removeEventListener(MouseEvent.MOUSE_DOWN, this.seriesItemMouseHandler);
                _loc_1++;
            }
            this.itemArray = null;
            while (this.numChildren > 0)
            {
                
                _loc_2 = getChildAt(0);
                this.removeChildAt(0);
                _loc_2 = null;
            }
            this.screenModel.removeStageMask();
            if (this.screenModel.stage.contains(this))
            {
                this.screenModel.stage.removeChild(this);
            }
            this.hasDestroyed = true;
            return;
        }// end function

        public function set scrollPanePosition(param1:Number) : void
        {
            if (this._serieScrollPane)
            {
                this._serieScrollPane.vScrollPosition = param1;
            }
            return;
        }// end function

        public function get scrollPanePosition() : Number
        {
            return this._serieScrollPane ? (this._serieScrollPane.vScrollPosition) : (0);
        }// end function

    }
}
